﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using AR.Entities;
using AR.ViewModels.Commands;
using System.Collections.ObjectModel;
using AR.Model;
using System.ServiceModel.DomainServices.Client;
using AR.Web;
using AR.CustomEntities;
using GalaSoft.MvvmLight.Command;
using System.Linq;
using AR.Web.Services;
using AR.CientFramework;

namespace AR.ViewModels
{
    public class EntretienTechniqueFormViewModel : ViewModelBase
    {
    

        private int Duree_test = 0;
        private int _idTest;
        private CANDIDAT currentCandidat;
        private System.Windows.Threading.DispatcherTimer myDispatcherTimer;
        public int IdTest
        {
            get
            {
                return _idTest;
            }
            set
            {
                if (value != _idTest)
                {
                    _idTest = value;
                    OnPropertyChanged("IdTest");
                }
            }
        }


        private int _timer;
        public int Timer
        {
            get
            {
                return _timer;
            }
            set
            {
                if (value != _timer)
                {
                    _timer = value;
                    OnPropertyChanged("Timer");
                    if (currentCandidat != null)
                    {
                        StockageIsole.ModifierDurreeEntretienCandidat(currentCandidat.ID_CANDIDAT, Duree_test);
                        //currentCandidat.DUREE_TEST = Duree_test;
                        //contextCandidat.SubmitChanges();
                    }
                }
            }
        }

        private ObservableCollection<QUESTIONS> _questions = new ObservableCollection<QUESTIONS>();
        public ObservableCollection<QUESTIONS> Questions
        {
            get
            {
                return _questions;
            }
            set
            {
                if (value != _questions)
                {
                    _questions = value;
                    OnPropertyChanged("Questions");
                }
            }
        }
        private ObservableCollection<REPONSE_TEST_TECHNIQUE_CANDIDAT> _checkedChoix = new ObservableCollection<REPONSE_TEST_TECHNIQUE_CANDIDAT>();
        public ObservableCollection<REPONSE_TEST_TECHNIQUE_CANDIDAT> CheckedChoix
        {
            get
            {
                return _checkedChoix;
            }
            set
            {
                if (value != _checkedChoix)
                {
                    _checkedChoix = value;
                    OnPropertyChanged("CheckedChoix");
                }
            }
        }
        private readonly EntretienTechniquetModel model;
        private readonly EntretienTechniqueContext context;
        System.Windows.Interop.Content content;
        public int idcandidat { get; set; }
        public string  CodeTest { get; set; }
        private ObservableCollection<THEME> _themes = new ObservableCollection<THEME>();
        public ObservableCollection<THEME> Themes
        {
            get
            {
                return _themes;
            }
            set
            {
                if (value != _themes)
                {
                    _themes = value;
                    OnPropertyChanged("Themes");
                }
            }
        }
        public EntretienTechniqueFormViewModel(string codetest, System.Windows.Interop.Content ct)
        {
            (Application.Current.RootVisual as BusyIndicator).IsBusy = true;
            content = ct;
            //content.IsFullScreen = true;
            context = new EntretienTechniqueContext();
            model = new EntretienTechniquetModel();
            this.CodeTest = codetest;
           

            context.Load<TEST_TECHNIQUE>(context.GetTEST_TECHNIQUE_CANDIDATByTestCodeQuery(codetest)).Completed += (sender, arg) =>
            {
                IdTest = context.TEST_TECHNIQUEs.SingleOrDefault().ID_TEST;
             
                currentCandidat = context.TEST_TECHNIQUEs.SingleOrDefault().CANDIDAT.FirstOrDefault();
                this.idcandidat = currentCandidat.ID_CANDIDAT;
                this.LoadThemes();
                //if (currentCandidat.DUREE_TEST != null && currentCandidat.DUREE_TEST > 0)
                //    Duree_test += (int)currentCandidat.DUREE_TEST;
                int d = StockageIsole.RetournerDurreeEntretienCandidat(currentCandidat.ID_CANDIDAT);
                if (d != -1)
                    Duree_test += d;
                else
                    StockageIsole.AjouterDurreeEntretienCandidat(currentCandidat.ID_CANDIDAT, Duree_test);
            };

            //context.Load<CANDIDAT>(context.GetCandidatByCodeTestQuery(codetest)).Completed += (sender, arg) =>
            //{
            //    currentCandidat = context.CANDIDATs.SingleOrDefault();
            //    this.idcandidat = currentCandidat.ID_CANDIDAT;
            //    //if (currentCandidat.DUREE_TEST != null && currentCandidat.DUREE_TEST > 0)
            //    //    Duree_test += (int)currentCandidat.DUREE_TEST;
            //    int d = StockageIsole.RetournerDurreeEntretienCandidat(currentCandidat.ID_CANDIDAT);
            //    if (d != -1)
            //        Duree_test += d;
            //    else
            //        StockageIsole.AjouterDurreeEntretienCandidat(currentCandidat.ID_CANDIDAT, Duree_test);
            //};

            inittimer();
        }

        private void inittimer()
        {
            myDispatcherTimer = new System.Windows.Threading.DispatcherTimer();
            myDispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 1000); // 100 Milliseconds 
            myDispatcherTimer.Tick += new EventHandler(Each_Tick);
            myDispatcherTimer.Start();
        }


        // Raised every 100 miliseconds while the DispatcherTimer is active.
        public void Each_Tick(object o, EventArgs sender)
        {
            Timer = Duree_test++;
        }
        public void LoadThemes()
        {
            Themes.Clear();
            model.GetThemes(idcandidat).EntityAdded += OnEntityAdded;

        }
        private void OnEntityAdded(object sender,
                                 EntityCollectionChangedEventArgs<THEME> entityCollectionChangedEventArgs)
        {
            Themes.Add(entityCollectionChangedEventArgs.Entity);
        }


        public ICommand EndtestCommand
        {
            get
            {
                return new RelayCommand(() =>
                {
                    this.Setnotes();

                    content.IsFullScreen = false;
                });
            }
        }

        private void Setnotes()
        {
            Questions = new ObservableCollection<QUESTIONS>();
            CheckedChoix = new ObservableCollection<REPONSE_TEST_TECHNIQUE_CANDIDAT>();
            REPONSE_TEST_TECHNIQUE_CANDIDAT reponse = new REPONSE_TEST_TECHNIQUE_CANDIDAT();
            foreach (var theme in Themes)
            {
                foreach (var question in theme.QUESTIONS)
                {
                    Questions.Add(question);
                    foreach (var choix in question.CHOIX)
                    {
                        if (choix.IsChecked == true)
                        {
                            reponse = new REPONSE_TEST_TECHNIQUE_CANDIDAT();
                            reponse.ID_QUESTION = question.ID_QUESTION;
                            reponse.ID_TEST = IdTest;
                            reponse.ID_CHOIX = choix.IDCHOIX;
                            reponse.ID_CANDIDAT = idcandidat;
                            CheckedChoix.Add(reponse);
                            //   MessageBox.Show(choix.QUESTIONS.ID_QUESTION.ToString() + choix.CHOIX1);
                        }
                        //choix
                    }
                }
            }
            model.HandleTest(CheckedChoix, Questions, idcandidat, Duree_test);
            myDispatcherTimer.Stop();
            StockageIsole.SupprimerDurreeEntretienCandidat(currentCandidat.ID_CANDIDAT);
        }

        public ICommand ClickChoixCommand
        {
            get
            {
                return new RelayCommand(() =>
                {
                    this.ClickChoix();

                });
            }
        }
        private void ClickChoix()
        {
        }

    }
}
